package it.win4buy.services
{
    import flash.events.IEventDispatcher;

    import it.win4buy.entities.UserDataVO;
    import it.win4buy.events.GetFlashVarsEvent;
    import it.win4buy.utility.LoggerUtilty;

    import mx.core.FlexGlobals;

    import spark.components.Application;




    /**
     * 	Read the FlashVars.
     *
     *	autore:
     *  pierangelo.mancusi@gmail.com
     *
     */

    public class GetFlashVarsService
    {

        //-------------------------------------------
        //
        // Injected properties
        //
        //-------------------------------------------

        [Dispatcher]
        public var dispatcher:IEventDispatcher;


        [Inject]
        public var log:LoggerUtilty;

        //-------------------------------------------
        //
        // Private Properties
        //
        //-------------------------------------------

        private var _application:Application;


        //-------------------------------------------
        //
        // Getters / Setters
        //
        //-------------------------------------------


        //-------------------------------------------
        //
        // Constructor
        //
        //-------------------------------------------

        public function GetFlashVarsService()
        {

        }

        //-------------------------------------------
        //
        // Mediator Handlers
        //
        //-------------------------------------------

        //-------------------------------------------
        //
        // Public Methods
        //
        //-------------------------------------------


        /**
         *  Read the Application Flash Vars
         *
         */
        public function execute(app:Application):void
        {

            //reference to the main application
            _application=app;

            var userId:String=_application.parameters["userId"];
            var applicationMode:String=_application.parameters["applicationMode"];



            if (userId != null)
            {
                //result
                log.logToConsole(this, "userId  present.");

                dispatcher.dispatchEvent(new GetFlashVarsEvent(GetFlashVarsEvent.USERID_RESULT, userId, true));
            }
            else
            {
                //fault
                log.logToConsole(this, "userId not present");
                dispatcher.dispatchEvent(new GetFlashVarsEvent(GetFlashVarsEvent.USERID_FAULT, userId, true));

            }

            if (applicationMode != null)
            {
                //result
                log.logToConsole(this, "applicationMode  present.");

                dispatcher.dispatchEvent(new GetFlashVarsEvent(GetFlashVarsEvent.APPLICATION_MODE_RESULT, applicationMode, true));
            }
            else
            {
                //fault
                log.logToConsole(this, "applicationMode not present");
                dispatcher.dispatchEvent(new GetFlashVarsEvent(GetFlashVarsEvent.APPLICATION_MODE_FAULT, applicationMode, true));

            }




        }

        //-------------------------------------------
        //
        // Private Methods
        //
        //-------------------------------------------


    }
}